<?php
/**
 * @package		Joomla.Site
 * @subpackage	mod_articles_
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

require_once JPATH_SITE.'/components/com_content/helpers/route.php';
require_once JPATH_SITE.'/components/com_content/helpers/image.php';

JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');

abstract class modArticlesIntroHelper
{
	public static function getList(&$params)
	{
		// Get an instance of the generic articles model
		$model = JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request' => true));
      
		// Set application parameters in model
		$app = JFactory::getApplication();
		$appParams = $app->getParams();
    
		$model->setState('params', $appParams);
    $item = $model->getItem($params->get('id'));
    $img = '';
    $images = json_decode($item->images);
    if (isset($images->image_intro) and !empty($images->image_intro))
        $img = $images->image_intro;
      elseif (isset($images->image_fulltext) and !empty($images->image_fulltext))
        $img = $images->image_fulltext;
      else{
        $regex   = "/<img[^>]+src\s*=\s*[\"']\/?([^\"']+)[\"'][^>]*\>/";
        $search  = $item->introtext;
        preg_match ($regex, $search, $matches);
        $images = (count($matches)) ? $matches : array();
        if ( count($images) ) {
          $img  = $images[1];
        }
      }
      if ($img) {
        $imageProcess = new ImageProcess($img,JPATH_BASE . DS . "images",JURI::base(false) . 'images/', $item->id, 694, 227,'crop');
        $attribs = array();
        $item->image = $imageProcess->getThumbnail();
      }
      else{
        $item->image = '';
      }
		return $item;
	}
}
